package com.yq.homework.day30.udp1;




import java.net.DatagramPacket;
import java.net.InetAddress;
import java.net.UnknownHostException;

/**
 * @author 3307235355@qq.com
 * @description
 * @since 2022/06/30 20:14
 */
public class NetUtils {
    private NetUtils() {
    }

    // 发送数据
    public static DatagramPacket getSendPacket(String msg, String ip, int port) throws UnknownHostException {
        byte[] bytes = msg.getBytes();
        InetAddress targetId = InetAddress.getByName(ip);
        DatagramPacket datagramPacket;
        datagramPacket = new DatagramPacket(bytes, 0, bytes.length, targetId, port);
        return datagramPacket;
    }

    //创建接收
    public static DatagramPacket getReceivePacket() {
        byte[] bytes = new byte[1024];
        DatagramPacket datagramPacket = new DatagramPacket(bytes, 0, bytes.length);
        return datagramPacket;
    }

    //数据的解析
    public static String parse(DatagramPacket datagramPacket){
        byte[] data = datagramPacket.getData();
        int length = datagramPacket.getLength();
        int offset = datagramPacket.getOffset();
        String s = new String(data, offset, length);
        return s;
    }
}
